package cn.iocoder.yudao.module.aiposter.controller.app.template;

import cn.iocoder.yudao.module.aiposter.service.template.AppTemplateService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.aiposter.controller.app.template.vo.*;
import cn.iocoder.yudao.module.aiposter.dal.dataobject.template.TemplateDO;

@Tag(name = "海报APP - 模版信息")
@RestController
@RequestMapping("/ai-poster/template")
@Validated
public class AppTemplateController {

    @Resource
    private AppTemplateService appTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建海报模版")
    public CommonResult<Long> createTemplate(@Valid @RequestBody AppTemplateSaveReqVO createReqVO) {
        return success(appTemplateService.createTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新海报模版")
    public CommonResult<Boolean> updateTemplate(@Valid @RequestBody AppTemplateSaveReqVO updateReqVO) {
        appTemplateService.updateTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除海报模版")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTemplate(@RequestParam("id") Long id) {
        appTemplateService.deleteTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得海报模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<AppTemplateRespVO> getTemplate(@RequestParam("id") Long id) {
        TemplateDO template = appTemplateService.getTemplate(id);
        return success(BeanUtils.toBean(template, AppTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得海报模版分页")
    public CommonResult<PageResult<AppTemplateRespVO>> getTemplatePage(@Valid AppTemplatePageReqVO pageReqVO) {
        PageResult<TemplateDO> pageResult = appTemplateService.getTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出海报信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportTemplateExcel(@Valid AppTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TemplateDO> list = appTemplateService.getTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模版信息.xls", "数据", AppTemplateRespVO.class,
                        BeanUtils.toBean(list, AppTemplateRespVO.class));
    }

}